package com.test.service.Impl;

import java.util.HashSet;
import java.util.Set;

import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.stereotype.Service;

import com.test.model.Permission;
import com.test.model.Role;
import com.test.model.User;
import com.test.service.PermissionService;

@Service
@EnableJpaAuditing
public class PermissionServiceImpl implements PermissionService {

	@Override
	public Set<String> getUserPermissionCodes(User user) {
		Set<Permission> permissions = getUserPermissions(user);
		Set<String> permissionCodes = new HashSet<String>();
		for (Permission permission : permissions) {
			permissionCodes.add(permission.getCode());
		}
		return permissionCodes;
	}

	public Set<Permission> getUserPermissions(User user) {
		Set<Permission> permissions = new HashSet<Permission>();
		for (Role role : user.getRoles()) {
			permissions.addAll(role.getPermissions());
		}
		return permissions;
	}
}
